package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //      map方法:转换流里面的数据类型
    //      collect(Collector collector)   收集流中的数据，放到集合中（list set Map）

    @Autowired
    private DishFlavorService dishFlavorService;

    //操作两张表，新增菜品，同时插入菜品对应的口味数据
    @Override
    @Transactional      //添加事务注解保证数据一致
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到dish
            this.save(dishDto);



        //获取菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((itm) -> {
            itm.setDishId(dishId);
            return itm;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dishFlavor
        dishFlavorService.saveBatch(flavors);//saveBatch保存集合，批量保存

    }


    //根据id查询菜品信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        //创建DishDto对象进行拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品口味信息,从dishFlavor表中查询
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lqw);
        dishDto.setFlavors(list);

        return dishDto;
    }

    //操作两张表，跟新菜品表和口味表
    @Override
    @Transactional      //添加事务注解保证数据一致
    public void updateWithFlavor(DishDto dishDto) {
        //更新地上表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据——dishFlavor表的delete操作
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(lqw);

        //添加当前提交过来的口味数据——dishFlavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((itm) -> {
            itm.setDishId(dishDto.getId());
            return itm;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    //TODO 操作两张表，菜品表和套餐表
    @Override
    public void updateWithSetmeal(List<Long> ids) {
        //判断菜品是否关联套餐，如果关联则不可停售
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getCategoryId,ids);

        this.count(lqw);
        if (count() > 0){
            throw new CustomException("该菜品已关联套餐，不能修改");
        }

        for (Long id : ids) {
            Dish byId = this.getById(id);
            if (byId.getStatus() != 0){
                byId.setStatus(0);
            }else {
                byId.setStatus(1);
            }
            //将修改后的数据写回
            this.updateById(byId);
        }

    }
}
